#include "Renderer.h"

// C-like function private and invisible outside
void convertMATRIXtoMat4(const MATRIX& matIn, glm::mat4& matOut )
{
	matOut = glm::mat4(1.0f);

	matOut[0][0] = matIn._11;
	matOut[1][0] = matIn._12;
	matOut[3][0] = matIn._13;

	matOut[0][1] = matIn._21;
	matOut[1][1] = matIn._22;
	matOut[3][1] = matIn._23;
}

Renderer::Renderer()
{
	memset(m_matrixStack, 0, sizeof(glm::mat4) * MATRIX_STACK_SIZE );
	m_matrixStackIndex = 0;

	// set first at Identity
	m_matrixStack[m_matrixStackIndex] = glm::mat4(1.0f);
}

void Renderer::pushMatrix( const MATRIX& transform )
{
	assert( m_matrixStackIndex < MATRIX_STACK_SIZE-1 );

	glm::mat4 Model;
	convertMATRIXtoMat4(transform, Model);
	m_matrixStack[m_matrixStackIndex + 1] = m_matrixStack[m_matrixStackIndex] * Model;
	m_matrixStackIndex++;
}

void Renderer::popMatrix()
{
#ifdef _DEBUG
	assert( m_matrixStackIndex > 0 );
	if(m_matrixStackIndex > 0){
	m_matrixStack[m_matrixStackIndex] = glm::mat4(1.0f);
#endif
	m_matrixStackIndex--;
#ifdef _DEBUG
	}
#endif
}

